package tree;

/**
 * 二叉树的最小深度
 */
public class MinimumDepthOfBinaryTree {

    private static class TreeNode {
        int value;
        TreeNode left;
        TreeNode right;
        TreeNode(int value) {
            this.value = value;
        }
        TreeNode(int value, TreeNode left, TreeNode right) {
            this.value = value;
            this.left = left;
            this.right = right;
        }
    }

    private int minDepth = Integer.MAX_VALUE;

    private int currentDepth = 0;

    public int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }

        // 从根节点开始进行DFS遍历
        traverse(root);
        return minDepth;
    }

    private void traverse(TreeNode root) {
        if (root == null)
            return;
        currentDepth++;
        if (root.left == null && root.right == null ) {
            minDepth = Math.min(minDepth, currentDepth);
        }
        traverse(root.left);
        traverse(root.right);
    }
}
